package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TdmTaskRelationMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.TaskRelation;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.TaskRelationVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.TaskRelationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liyi on 2017/3/1.
 * 关联任务 业务层
 */
public class TaskRelationServiceImpl implements TaskRelationService {

    @Autowired
    public TdmTaskRelationMapper tdmTaskRelationMapper;

    /**
     * 保存关联任务信息
     * @param taskRelation
     * @return
     */
    @Override
    public int save(TaskRelation taskRelation) {
        if (taskRelation == null) {
            return 0;
        }
        taskRelation.setTaskRelationId(UniqueUtil.uuid());
        return tdmTaskRelationMapper.insertSelective(taskRelation);
    }

    /**
     * 取分析处理关联任务信息
     * @param taskRelationVo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> listMap(TaskRelationVo taskRelationVo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        taskRelationVo.setOffset( taskRelationVo.getOffset() == null ? 0 : taskRelationVo.getOffset() );
        taskRelationVo.setLimit( taskRelationVo.getLimit() == null ? 10 : taskRelationVo.getLimit() );
        map.put("total", tdmTaskRelationMapper.queryCount(taskRelationVo));
        map.put("rows", JSON.toJSON(tdmTaskRelationMapper.queryList(taskRelationVo)));
        return map;
    }

    /**
     * 取变更管理关联十大任务信息
     * @param taskRelationVo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> listChangeManageMap(TaskRelationVo taskRelationVo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        taskRelationVo.setOffset( taskRelationVo.getOffset() == null ? 0 : taskRelationVo.getOffset() );
        taskRelationVo.setLimit( taskRelationVo.getLimit() == null ? 10 : taskRelationVo.getLimit() );
        map.put("total", tdmTaskRelationMapper.queryChangeManageCount(taskRelationVo));
        map.put("rows", JSON.toJSON(tdmTaskRelationMapper.queryChangeManageList(taskRelationVo)));
        return map;
    }

    /**
     * 取危害因素辨识关联十大任务信息
     * @param taskRelationVo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> listHazardIdentificationMap(TaskRelationVo taskRelationVo) {
        Map<String, Object> map = new HashMap<>();
        taskRelationVo.setOffset( taskRelationVo.getOffset() == null ? 0 : taskRelationVo.getOffset() );
        taskRelationVo.setLimit( taskRelationVo.getLimit() == null ? 10 : taskRelationVo.getLimit() );
        map.put("total", tdmTaskRelationMapper.queryChangeManageCount(taskRelationVo));
        map.put("rows", JSON.toJSON(tdmTaskRelationMapper.queryChangeManageList(taskRelationVo)));
        return map;
    }

    @Override
    public List<TaskRelationVo> selectNameByMasterTaskId(String id) {
        return tdmTaskRelationMapper.selectNameByMasterTaskId(id);
    }

}
